package cn.lcj.org.controller;

import cn.lcj.basic.utils.AjaxResult;
import cn.lcj.basic.utils.PageList;
import cn.lcj.org.domain.Employee;
import cn.lcj.org.domain.Employee;
import cn.lcj.org.query.EmployeeQuery;
import cn.lcj.org.service.IEmployeeService;
import cn.lcj.org.service.impl.EmployeeServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * $
 *
 * @return $
 * @date $ $
 * @description $
 */
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    /**
     * 查询单个 用get  url路径传参
     * @PathVariable 获取路径参数  不加获取不了数据
     * @param id
     * @return
     */
    @ApiOperation(value = "接口：查询单个")
    @GetMapping("/{id}")
    public Employee findOne(@PathVariable Long id){
        return employeeService.loadById(id);
    }

    /**
     * 查询全部
     * @return
     */
    @ApiOperation(value = "接口：查询全部")
    @GetMapping
    //http://localhost:8080/employee
    public List <Employee> findAll(){
        return  employeeService.loadAll();
    }

    /**
     * 返回值需要用AjaxResult接收
     * 删除
     * @PathVariable url上获取参数
     * @param id
     * @return
     */
    @ApiOperation(value = "接口：删除")
    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            employeeService.del(id);
            return  new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
     * 新增与修改
     * 返回值需要用AjaxResult接收 需要返回操作信息
     * 前端对象的接收需要用@RequestBody 从请求体里面获取数据
     * @param employee
     * @return
     */
    @ApiOperation(value = "接口：新增与修改")
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        try {
            if (employee.getId()==null){
                employeeService.save(employee);
                return new AjaxResult();
            }else {
                employeeService.update(employee);
                return new AjaxResult();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }

    }

    /**
     * 分页查询
     * 用封装类的对象做参数来接收
     * 前端对象的接收需要用@RequestBody 从请求体里面获取数据
     * @param query
     * @return
     */
    @ApiOperation(value = "接口：分页查询")
    @PostMapping
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery query){
        return employeeService.queryPage(query);
    }

    /**
     * 返回值需要用AjaxResult接收
     * 批量删除
     * @PathVariable url上获取参数
     * @param ids
     * @return
     */
    @ApiOperation(value = "接口：批量删除")
    @PatchMapping
    public AjaxResult patchList(@RequestBody List<Long> ids){
        try {
            employeeService.patchDelete(ids);
            return  new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }
}
